package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.utils.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * 提现账号
 */
public class GetAccountActivity extends BaseActivity {

    @BindString(R.string.get_account_text)
    String title;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.account_list_parent)
    LinearLayout accountListParent;
    @Bind(R.id.pay_account)
    EditText payAccount;
    @Bind(R.id.pay_password)
    EditText payPassword;
    @Bind(R.id.submit_account)
    TextView submitAccount;
    @Bind(R.id.activity_get_account)
    LinearLayout activityGetAccount;
    @Bind(R.id.account_current)
    TextView accountCurrent;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_get_account;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);

    }

    @Override
    protected void setUpViews() {
        String zhifubaoStr = UserAPI.getCurrentUser().getZhifubao();
        if (TextUtils.isEmpty(zhifubaoStr)) {
            accountListParent.setVisibility(View.GONE);
        } else {
            accountListParent.setVisibility(View.VISIBLE);
            accountCurrent.setText(zhifubaoStr);
        }
    }

    @OnClick(R.id.submit_account)
    public void submitAccount(View v) {
        if (TextUtils.isEmpty(payAccount.getText().toString())) {
            T.show(this, "请输入账号", Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(payPassword.getText().toString())) {
            T.show(this, "请输入支付密码", Toast.LENGTH_SHORT);
            return;
        }
        if (isNetvaiable()) {
            UserAPI.editUserPayAccount(payAccount.getText().toString(), this, getLoading(-1));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);

        BaseModel base = validateJson(json);
        if (base.isSuccess()) {
            UserAPI.getCurrentUser().setZhifubao(payAccount.getText().toString());
            EventBus.getDefault().post(new EventBusEntity(EventBusEntity.Type.ACCOUNT_NAME));
            finish();
        }
    }
}
